package servlet;

import dao.CategoryDao;
import dao.daoimpl.CategoryDaoImpl;
import bean.Category;
import bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name="UpdateCategoryServlet",urlPatterns = "/UpdateCategoryServlet")
public class UpdateCategoryServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");/*防止乱码*/
        User user = (User)request.getSession().getAttribute("user");
        int id=Integer.parseInt(request.getParameter("updateArticleCategoryId"));/*获取目录id*/
        String intro=request.getParameter("updateArticleCategoryIntro");/*获取描述*/
        String name=request.getParameter("updateArticleCategoryName");/*获取名称*/
        String time= new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());/*获取当前时间*/
        CategoryDao categoryDao=new CategoryDaoImpl();/*目录接口的实现类*/
        if(categoryDao.updateCategory(new Category(id,name,time,intro))){
            System.out.println("用户修改目录成功");
            try {
                request.getSession().setAttribute("userCategories",categoryDao.getCategories(user));
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        }else{
            System.out.println("用户修改目录失败");
        }
    }
}
